<?php

namespace App\Listeners;

use App\Events\GoodsWatched;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;

use App\Models\GoodsWatchLogs;
class GoodsWatchedNotification
{

    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {


    }

    /**
     * Handle the event.
     *
     * @param  GoodsWatched  $event
     * @return void
     */
    public function handle(GoodsWatched $event)
    {
     $goodsWatchLog = $event->goods->goodsWatchLogs()
                    ->where("ip_addr",$event->ip)
                    ->orderBy("id","desc")
                    ->first();
     //如果同一个IP在10分钟之内访问同一个产品则归为一次访问
     if (!$goodsWatchLog || (time() - strtotime($goodsWatchLog->created_at)>600)){
         $event->goods->goodsWatchLogs()->create([
             "ip_addr"=>$event->ip,
             "user_id"=>$event->user,
         ]);
         $event->goods->increment('click_number');
     }

    }
}
